package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import dto.AgingTestQueryDTO;
import dto.InspectionQualityQueryDTO;
import entity.AgingTest;
import entity.QualityInspection;
import entity.QualityStandard;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.example.constant.InspectionConstant;
import org.example.constant.JwtClaimsConstant;
import org.example.properties.JwtProperties;
import org.example.repository.InspectorRepository;
import org.example.result.PageResult;
import org.example.service.InspectorService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class InspectorServiceImpl implements InspectorService {

    private final InspectorRepository inspectorRepository;

    /**
     * 查询质检单
     * 
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult queryQualityInspection(InspectionQualityQueryDTO queryDTO) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(JwtClaimsConstant.USER_ID);
        queryDTO.setInspectorId(userId);
        // 开始分页查询
        PageHelper.startPage(queryDTO.getPageNumber(), queryDTO.getPageSize());
        Page<QualityInspection> qualityInspections = inspectorRepository.queryQualityInspection(queryDTO);
        return new PageResult(qualityInspections.getTotal(), qualityInspections.getResult());
    }

    /**
     * 更新质检单
     *
     * @param qualityInspection
     */
    @Override
    public void updateQualityInspection(QualityInspection qualityInspection) {
        qualityInspection.setUpdateTime(LocalDateTime.now());
        inspectorRepository.updateQualityInspection(qualityInspection);
    }

    /**
     * 分页查询老化测试
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult queryAgingTest(AgingTestQueryDTO queryDTO) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(JwtClaimsConstant.USER_ID);
        queryDTO.setEngineerId(userId);
        // 开始分页查询
        PageHelper.startPage(queryDTO.getPageNumber(), queryDTO.getPageSize());
        Page<AgingTest> agingTests = inspectorRepository.queryAgingTest(queryDTO);
        return new PageResult(agingTests.getTotal(), agingTests.getResult());
    }

    /**
     * 添加老化测试
     *
     * @param agingTest
     */
    @Override
    public void addAgingTest(AgingTest agingTest) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get(JwtClaimsConstant.USER_ID);
        agingTest.setEngineerId(userId);
        agingTest.setTestStatus(InspectionConstant.TESTING);
        agingTest.setCreateTime(LocalDateTime.now());
        agingTest.setUpdateTime(LocalDateTime.now());
        inspectorRepository.addAgingTest(agingTest);
    }

    /**
     * 更新老化测试
     *
     * @param agingTest
     */
    @Override
    public void updateAgingTest(AgingTest agingTest) {
        agingTest.setUpdateTime(LocalDateTime.now());
        inspectorRepository.updateAgingTest(agingTest);
    }

    /**
     * 成功老化测试
     * @param agingTest
     */
    @Override
    public void successAgingTest(AgingTest agingTest) {
        agingTest.setUpdateTime(LocalDateTime.now());
        agingTest.setTestStatus(InspectionConstant.TESTED);
        inspectorRepository.updateAgingTest(agingTest);
    }

    /**
     * 查询质检标准
     * @param inspectCategory
     * @return
     */
    @Override
    public List<QualityStandard> listQualityStandard(String inspectCategory) {
        return inspectorRepository.listQualityStandard(inspectCategory);
    }
}
